package com.cody.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Cody
 * @date 2021/4/6 - 20:35
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //执行完毕，返回前拦截
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //在处理过程中进行拦截
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*//在执行前拦截*/
        HttpSession session = request.getSession();
        if( session.getAttribute("user")==null||session.getAttribute("user")==" "){
            request.setAttribute("msg","您没有权限，请先进行登录");
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }else{

            return true ;
        }
    }
}
